﻿using CPPEI.Coolzon.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.DataProvider.Cache
{
    class DataSetCache
    {
        private List<DataParameterCache> _dataParameterList;
        private object _lock = new object();

        public string Name { get; private set; }

        public DataSetCache(string name)
        {
            this.Name = name;
            _dataParameterList = new List<DataParameterCache>();
        }

        public DataParameterCache Get(string dataSetName)
        {
            for (int index = 0; index < _dataParameterList.Count; index++)
            {
                if (_dataParameterList[index].Name == dataSetName)
                {
                    return _dataParameterList[index];
                }
            }
            return null;
        }

        public DataParameterCache Store(string dataSetName, DataResult dataResult)
        {
            DataParameterCache cache = this.Get(dataSetName);
            if (cache == null)
            {
                lock (_lock)
                {
                    cache = this.Get(dataSetName);
                    if (cache == null)
                    {
                        cache = new DataParameterCache(dataSetName, dataResult);
                        _dataParameterList.Add(cache);
                        return cache;
                    }
                }
            }
            return cache;
        }
    }
}
